# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=lua-dbi
_pkgname=luadbi
pkgver=0.7.2
pkgrel=1
pkgdesc="database interface library for Lua"
url="https://github.com/mwild1/luadbi"
arch="all"
license="MIT"
makedepends="mariadb-connector-c-dev libpq-dev sqlite-dev"
_subpackages="$pkgname-mysql $pkgname-postgresql $pkgname-sqlite3"
source="https://github.com/mwild1/$_pkgname/archive/v$pkgver/$_pkgname.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"
options="!check"  # provided tests require running DBs

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	for _subpkg in $_subpackages; do
		subpackages="$subpackages lua$_v-${_subpkg#lua-}:_subpackage"
	done
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."
		make -C "$builddir-$lver" LUA_V=$lver
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	local db=${subpkgname##*-}
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname-$db/$pkgver-2"
	pkgdesc="$pkgdesc $lver and $db"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	cd "$builddir-$lver"
	make LUA_V=$lver DESTDIR="$subpkgdir" install_${db/postgresql/psql}

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="891f99cbc8f1b5327ef820641a1608abf41410f2f28584b975cc3cf65154b9bebf6f1b9ca7d1818b9c965738ad700dd17ae042dfd2a7146cf755871ebae535b9  luadbi.tar.gz"
